﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.Mvc;
using AMing.Extension.Net40;
using AMing.Web.Net40.Extension;
using Html5Music.Data;

namespace MusicForMvc3.Method
{
    public class BaseController : Controller
    {
        #region Data

        public const string Admin_Access_CookieNameKey = "Aming.Admin_Access_CookieNameKey";
        public const string Admin_Access_CookieValueKey = "Aming.Admin_Access_CookieValueKey";

        /// <summary>
        /// 超级管理员账号
        /// </summary>
        public static string SuperAdmin_ID = WebConfigurationManager.AppSettings["Admin_ID"];
        /// <summary>
        /// 超级管理员密码
        /// </summary>
        public static string SuperAdmin_Pwd = WebConfigurationManager.AppSettings["Admin_Pwd"];

        /// <summary>
        /// 权限
        /// </summary>
        public string Admin_Access
        {
            get
            {
                return this.Request.AESCookie_Get(Admin_Access_CookieNameKey, Admin_Access_CookieValueKey);
            }
            set
            {
                this.Response.AESCookie_Set(Admin_Access_CookieNameKey, Admin_Access_CookieValueKey, val: value);
            }
        }

        public Html5Music.Model.User UserData
        {
            get
            {
                string username = this.HttpContext.User.Identity.Name;

                return Userlist.GetByLoginName(username);
            }
        }

        public bool IsLogin
        {
            get
            {
                return this.HttpContext.User.Identity.IsAuthenticated;
            }
        }

        private string _viewPath = "~/Views/Main/{0}.cshtml";

        protected string ViewPath
        {
            get { return _viewPath; }
            set { _viewPath = value; }
        }

        #endregion

        #region Method

        protected void Loguot()
        {
            this.Response.Cookie_Delete(Admin_Access_CookieNameKey);
        }


        /// <summary>
        /// 加载postdata
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        protected T LoadPostData<T>(T data)
        {
            Type t_b = typeof(T);

            var props = t_b.GetProperties();

            foreach (var item in props)
            {
                string val = this.Request.Form[item.Name];
                if (val != null)
                {
                    try
                    {
                        object obj_val = Convert.ChangeType(val, item.PropertyType);
                        item.SetValue(data, obj_val, null);
                    }
                    catch { }
                }
            }

            return data;
        }

        /// <summary>
        /// 前台视图结果基方法
        /// </summary>
        protected ActionResult ViewPage()
        {
            string actionName = this.ControllerContext.RouteData.GetRequiredString("action");

            return ViewPage(actionName);
        }

        /// <summary>
        /// 前台视图结果基方法
        /// </summary>
        protected ActionResult ViewPage(string viewPageName)
        {
            ViewBag.IsLogin = this.IsLogin;
            ViewBag.User = this.UserData;

            return View(this.ViewPath.ToFromat(viewPageName));
        }

        #endregion

        #region Data
        private PlayListData _playlist = null;

        public PlayListData Playlist
        {
            get
            {
                if (_playlist == null)
                {
                    Html5Music.Data.Config.Path_PlayList = Server.MapPath("~/file/playlist.txt");
                    _playlist = new PlayListData();
                }
                return _playlist;
            }
        }
        private UserListData _userlist = null;

        public UserListData Userlist
        {
            get
            {
                if (_userlist == null)
                {
                    Html5Music.Data.Config.Path_UserList = Server.MapPath("~/file/userlist.txt");
                    _userlist = new UserListData();
                }
                return _userlist;
            }
        }
        #endregion
    }
}